package com.example.springboot.service.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.ActivityMapper;
import com.example.springboot.dao.RecordMapper;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Activity;
import com.example.springboot.domain.po.Record;
import com.example.springboot.exception.CustomException;
import com.example.springboot.exception.GlobalExceptionHandler;
import com.example.springboot.service.RecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：RecordServiceImpl
 * @Date：2025/2/23 21:18
 * @Filename：RecordServiceImpl
 * @for:
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {
    @Resource
    private RecordMapper recordMapper;
    @Resource
    private ActivityMapper activityMapper;

    @Override
    public List<Record> GetAll() {
        return this.lambdaQuery().list();
    }

    @Override
    public Record recordRegister(Record record) {
        record.setTime(DateUtil.now());
        recordMapper.insert(record);
        return record;
    }


    @Override
    public Record findById(Integer id) {
        return recordMapper.selectById(id);
    }

    @Override
    public void delete(Integer id) {
        recordMapper.deleteById(id);
    }

    @Override
    public PageInfo<Record> findPage(Record search, Integer pageNum, Integer pageSize) {
        Account user = JwtTokenUtils.getCurrentUser();
        if (ObjectUtil.isEmpty(user)) {
            throw new CustomException(ResultCode.USER_NOT_LOGIN);
        }
        List<Record> all = null;
        try {
            if (ObjectUtil.isNotEmpty(search.getActivityName())) {
                Integer id = this.selectActivityIdByActivityName(search.getActivityName());
                search.setActivityId(id);
            }
            PageHelper.startPage(pageNum, pageSize);
            all = findByCondition(search, user);
        } catch (Exception e) {
            throw new CustomException(ResultCode.TITLE_ISERROR);
        }

            return PageInfo.of(all);
    }

    @Override
    public List<Record> findByCondition(Record search, Account user) {

        if (1 == user.getRole()) {
            return recordMapper.findBySearch(search, null, null);
        } else {
            return recordMapper.findBySearch(search, user.getId(), user.getRole());
        }


    }

    private Integer selectActivityIdByActivityName(String activityName) {
        QueryWrapper<Activity> wrapper = new QueryWrapper<Activity>()
                .like("title", activityName);
        Activity activity = activityMapper.selectOne(wrapper);
        return activity.getId();

    }

    @Override
    public Record recordEdit(Record record) {
        recordMapper.updateById(record);
        return record;
    }


}
